package com.lin.fengxiu.service;

import com.lin.fengxiu.core.enumeration.CouponStatus;
import com.lin.fengxiu.exception.http.NotFoundException;
import com.lin.fengxiu.exception.http.ParameterException;
import com.lin.fengxiu.model.Activity;
import com.lin.fengxiu.model.Coupon;
import com.lin.fengxiu.model.UserCoupon;
import com.lin.fengxiu.repository.ActivityRepository;
import com.lin.fengxiu.repository.CouponRepository;
import com.lin.fengxiu.repository.UserCouponRepository;
import com.lin.fengxiu.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName CouponService
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/24 22:09
 * @Version 1.0
 */
@Service
public class CouponService {

    @Autowired
    private ActivityRepository activityRepository;

    @Autowired
    private CouponRepository couponRepository;

    @Autowired
    private UserCouponRepository userCouponRepository;

    /**
     *
     * @param  cid TODO 分类id，用来查询所属的优惠券
     * @return
     */
    public List<Coupon> getByCategory(Long cid) {
        // 传入的Date用于过滤过期的优惠券
        Date now = new Date();
        return couponRepository.findByCategory(cid,now);
    }

    /**
     * @Description TODO 全场券查询
     * @return
     */
    public List<Coupon> getWholeStoreCoupons() {
        // 传入的Date用于过滤过期的优惠券
        Date now = new Date();
        boolean isWholeStore = true;
        return couponRepository.findByWholeStore(isWholeStore,now);

    }

    /**
     * @Description TODO 获取未使用优惠券
     * @param uid
     * @return
     */
    public List<Coupon> getMyAvailableCoupons(Long uid){
        Date now = new Date();
        return this.couponRepository.findMyAvailable(uid,now);
    }
    /**
     * @Description TODO 获取已使用优惠券
     * @param uid
     * @return
     */
    public List<Coupon> getMyUsedCoupons(Long uid){
        Date now = new Date();
        return this.couponRepository.findMyUsed(uid,now);
    }

    /**
     * @Description TODO 获取已过期优惠券
     * @param uid
     * @return
     */
    public List<Coupon> getMyExpiredCoupons(Long uid){
        Date now = new Date();
        return this.couponRepository.findMyExpired(uid,now);
    }

    /**
     * @Description TODO 領取優惠券
     * @param uid  领取优惠券的用户
     * @param couponId  当前用户要领取的优惠券
     */
    public void collectOneCoupon(Long uid, Long couponId){
        // 查询优惠券是否存在
        this.couponRepository
                .findById(couponId)
                .orElseThrow(()-> new NotFoundException(40001));
        // 查询当前优惠券是否过期，优惠券的过期与活动有关
        // 1.查询优惠券所属的活动是否存在
        Activity activity = this.activityRepository.findByCouponListId(couponId)
                .orElseThrow(()-> new NotFoundException(40001));

        // 2.判断活动时间是否过期
        Date now = new Date();
        Boolean isIn = CommonUtil.isInTimeLine(now, activity.getStartTime(),activity.getEndTime());
        System.out.println("ISIN -------------");
        if(!isIn){
            throw new ParameterException(40006);
        }
        // 3.用户是否领取过优惠券，查询当前用户是否有与当前couponId一致的优惠券
        Optional<UserCoupon> userCoupon = userCouponRepository
                .findFirstByUserIdAndCouponId(uid, couponId);
        if(userCoupon.isPresent()){
            throw new ParameterException(40006);
        }
//                .orElseThrow(()-> new ParameterException(40006));
        // 4.满足以上条件，为用户领取优惠券
        // ①创建用户领取优惠券关系模型
        UserCoupon userCouponNew = UserCoupon.builder()
                .userId(uid)
                .couponId(couponId)
                .status(CouponStatus.AVAILABLE.getValue())
                .createTime(now)
                .build();
        // ②保存用户领取优惠券关系模型数据，完成这一步，用户就领取到了优惠券
        userCouponRepository.save(userCouponNew);
    }
}
